<template>
  <div class="main">
    <section>
      <el-table
        v-loading="loading"
        :data="list"
        element-loading-text="给我一点时间"
        border
        fit
        header-row-class-name="table-header"
        highlight-current-row
      >
        <el-table-column align="center" label="平台id">
          <template slot-scope="scope">
            <span>{{scope.row.id}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="平台名称" width="130">
          <template slot-scope="scope">
            <span>{{scope.row.name}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="状态" width="210">
          <template slot-scope="scope">
            <span>{{scope.row.enabled===1?'启用':'停用'}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-switch
              style="display:block"
              v-model="scope.row.enabled"
              @change="updateStatus(scope.row)"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-text="启用"
              inactive-text="停用"
              :active-value="1"
              :inactive-value="0"
              :disabled="!rightsButtons['operate']"
            ></el-switch>
          </template>
        </el-table-column>
      </el-table>
    </section>
  </div>
</template>
<script>
import { getPlayform, updatePlatformStatus } from "@/api/bihuasuan/goods";
export default {
  name: "b-goodsPlatform",
  data() {
    return {
      list: [],
      loading: false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    async getList() {
      this.loading = true;
      try {
        const { data } = await getPlayform();
        this.loading = false;
        this.list = data;
      } catch (error) {
        console.log(error);
        this.list = [];
        this.loading = false;
      }
    },
    async updateStatus(val) {
      try {
        const { data } = await updatePlatformStatus({
          enabled: val.enabled,
          id: val.id
        });
        if (data) {
          this.$message.success("修改成功");
        } else {
          this.$message.error("修改失败");
        }
        this.getList();
      } catch (error) {
        this.$message.error("修改失败");
        this.getList();
        console.log(error);
      }
    }
  }
};
</script>
